<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2022/6/22
 * Time: 15:43
 */

namespace app\api\controller;


use app\project\service\PublicService;
use think\Controller;
use think\Exception;
use think\Session;

class Policy extends Controller
{

    //统计数量
    public function countApi()
    {
        $common = new Common();
        if (!empty($_POST['uid'])) {
            $project_user = $common->testusername($_POST['uid']);
        } else {
            $project_user = $this->iflogin();
        }
        try {
            $where['status'] = 1;
            $where['startTime'] = ['elt',date("Y-m-d")];
            $where['endTime'] = ['egt',date("Y-m-d")];
            $count = PublicService::counService("project_strategy", $where);

            //获取发布部门列表
            unset($where);
            $where['type'] = 9;
            $where['status'] = 1;
            $departmentList = PublicService::ListService("project_type", $where, "id,name",0,0,"sort desc");
            foreach ($departmentList as $k => $v) {
                $departmentList[$k]['active'] = false;
            }

            //获取企业规模列表
            unset($where);
            $where['type'] = 10;
            $where['status'] = 1;
            $scaleList = PublicService::ListService("project_type", $where, "id,name",0,0,"sort desc");
            foreach ($scaleList as $k => $v) {
                $scaleList[$k]['active'] = false;
            }

            //获取行业分类
            $where['type'] = 11;
            $industryList = PublicService::ListService("project_type", $where, "id,name",0,0,"sort desc");
            foreach ($industryList as $k => $v) {
                $industryList[$k]['active'] = false;
            }

            $black['code'] = 0;
            $black['count'] = $count;
            $black['departmentList'] = $departmentList;
            $black['industryList'] = $industryList;
            $black['scaleList'] = $scaleList;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);


    }

    //根据条件查询惠企政策列表
    public function ListApi()
    {
        $common = new Common();
        if (!empty($_POST['uid'])) {
            $project_user = $common->testusername($_POST['uid']);
        } else {
            $project_user = $this->iflogin();
        }
        try {
            $page = empty($_POST['page']) ? 1 : $_POST['page'];
            $limit = empty($_POST['limit']) ? 10 : $_POST['limit'];
            $where['status'] = 1;
            $where['startTime'] = ['elt',date("Y-m-d")];
            $where['endTime'] = ['egt',date("Y-m-d")];
            if (!empty($_POST['keyword'])) {
                $where['title'] = ['like', "%{$_POST['keyword']}%"];
            }

            if (!empty($_POST['selList'])) {
                $selList = json_decode($_POST['selList'], true);
                $selList = array_column($selList, "id");
                $typeWhere['id'] = ['in', $selList];
                $type= PublicService::ListService("project_type", $typeWhere, "id,name,type");

                $department = []; //发布部门
                $industry = []; //行业
                $scale = []; //企业
                foreach ($type as $k=>$v){
                    switch ($v['type']){
                        case 9:
                            $department[] = $v['id'];
                            break;
                        case 10:
                            $scale[] = $v['id'];
                            break;
                        case 11:
                            $industry[] = $v['id'];
                            break;
                    }
                }
                if(!empty($department)){
                    $where['department'] = ['in',$department];
                }
                if(!empty($industry)){
                    $where['industry'] = ['in',$industry];
                }
                if(!empty($scale)){
                    $where['scale'] = ['in',$scale];
                }
            }
            $returnBlack = PublicService::ListService("project_strategy", $where, null, $page, $limit);
            $list = $returnBlack['list'];
            foreach ($list as $k=>$v){
                $list[$k]['addtime'] = date("Y-m-d",$v['addtime']);
            }
            $count = $returnBlack['count'];
            $total = ceil($count / $limit);
            $black['code'] = 0;
            $black['list'] = $list;
            $black['count'] = $count;
            $black['total'] = $total;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //查看惠企政策详情
    public function detailApi(){
        $common = new Common();
        if (!empty($_POST['uid'])) {
            $project_user = $common->testusername($_POST['uid']);
        } else {
            $project_user = $this->iflogin();
        }
        try{
            $id = $_POST['id'];
            $where['id'] = $id;
            $common->public_select("project_strategy",$where)->setInc("browse");
            $detail = PublicService::detailService("project_strategy", $where,1);
            if(!empty($detail['path'])){
                $detail['path'] = "https://".$_SERVER['SERVER_NAME'].$detail['path'];
            }


            //查询部门名称
            $typeWhere['id'] = $detail['department'];
            $department = $common->public_select("project_type",$typeWhere)->value("name");
            $detail['department'] = empty($department)?"":$department;

            //查询规模分类
            $typeWhere['id'] = $detail['scale'];
            $scale = $common->public_select("project_type",$typeWhere)->value("name");
            $detail['scale'] = empty($scale)?"":$scale;

            //查询行业名称
            $typeWhere['id'] = $detail['industry'];
            $industry = $common->public_select("project_type",$typeWhere)->value("name");
            $detail['industry'] = empty($industry)?"":$industry;


            $detail['addtime'] = date("Y-m-d",$detail['addtime']);
            $black['code'] = 0;
            $black['detail'] = $detail;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    public function iflogin()
    {
        $project_user = Session::get("project_user");
        if (empty($project_user)) {
            $black['code'] = 1;
            $black['msg'] = "请重新登录";
            echo json_encode($black);
            die;
        }
        return $project_user;
    }
}